<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/24
 * Time: 17:04
 */

namespace Admin\Model;


use Admin\Common\Model\CommonModel;

class ClassTaskModel extends CommonModel
{
    protected $tableName = 'class_task';

    public function setTask($course_id){
        if (!$course_id)return false;
        $courseInfo = M('courses')->where(array('id'=>$course_id))->find();
        if ($courseInfo){
            $date = self::reckonDate($courseInfo['startime'],$courseInfo['stages']);
        }else{
            return false;
        }
        if ($date){
            foreach ($date as $key=>$val){
                $arr['class_date'] = $val;
                $arr['course_id'] = $course_id;
                $arr['is_sign'] = '0';
                $arr['is_leave'] = '0';
                $arr['value'] = '';
                $arr['sort'] = '第'.($key+1).'周';
                $arr['createtime'] = date('Y-m-d H:i:s',time());
                M("class_task")->add($arr);
            }
        }else{
            return false;
        }
        return true;
    }

    /**
     * 计算上课 时间
     * @param $nowdate
     * @param $num
     * @return mixed
     */
    public function reckonDate($nowdate ,$num){
        $nowdate = date("Y-m-d",strtotime($nowdate));
        if (strtotime($nowdate) < strtotime("0 week Sunday")){
            $firstWeek = date("Y-m-d",strtotime("0 week Sunday"));
        }
        for ($i=0;$i<$num;$i++){
            $date[$i] = date("Y-m-d",strtotime($firstWeek)+$i*7*24*60*60);
        }
        return $date;
    }
}